// bind_users.js
var app = getApp()
//常量数据配置
var ConstantData = require('../../../../common/constant_data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bindList:[],
    tag:'',
    callServiceBoxStatus:false,
    showUpdateTagStatus:false,
    bindid:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBindUsers()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      callServiceBoxStatus:false,
      showUpdateTagStatus:false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  setTag:function(e) {
    this.data.tag = e.detail.value
  },
  /**
   * 获取绑定用户
   */
  getBindUsers:function() {
    var that = this

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_time_card/binduser',
        header: header,
        data: {

        },
        success: function (res) {
          var data = res.data.data
          that.setData({
            bindList: data
          })
        },
        fail: function (res) {
          wx.showToast({
            title: '网络异常，稍后重试',
          })
        }
      })

    })  

  },
  /**
   * 弹框选择是否取消代租
   */
 unBindPopBox:function(e) {
      var bindid = e.currentTarget.dataset.bindid
      this.setData({
        callServiceBoxStatus:true,
        bindid:bindid
      })
  },
  /**
   * 取消"取消代租"
   */
  cancleSel:function(){
    this.setData({
      callServiceBoxStatus: false,
      bindid: 0
    })
  },
  /**
   * 解除绑定
   */
 unBind:function() {
    var that = this
    var bindid = this.data.bindid

   app.executeRequest(function (header) {

     wx.request({
       url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_time_card/unbind',
       header: header,
       data: {
         id: bindid
       },
       success: function (res) {
         that.setData({
           bindid: 0,
           callServiceBoxStatus: false
         })
         that.getBindUsers()
       },
       fail: function (res) {
         wx.showToast({
           title: '稍后重试',
         })
       }
     })

   })  

  },
  /**
   * 更新备注的弹框
   */
  updateTagPopBox:function(e){
    var bindid = e.currentTarget.dataset.bindid
     this.setData({
       showUpdateTagStatus:true,
       bindid: bindid
     })
  },
  /**
   * 取消更新备注弹框
   */
  cancelUpdateTag:function(){
    this.setData({
      showUpdateTagStatus: false,
      bindid: 0
    })
  },
  /**
   * 更新备注
   */
  updateTag:function() {
    var that = this
    var tag = this.data.tag
    if(tag == null || tag == '') {
      wx.showToast({
        title: '备注不能为空',
      })
      return
    }
    var bindid = this.data.bindid

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_time_card/updatetag',
        header: header,
        data: {
          bindId: bindid,
          tag: tag
        },
        success: function (res) {
          var result = res.data.result
          if (result == 0) {
            wx.showToast({
              title: '更新成功',
            })
            that.setData({
              showUpdateTagStatus: false,
              bindid: 0
            })
            that.getBindUsers()
          } else {
            wx.showToast({
              title: '更新失败',
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '网络异常，稍后重试',
          })
        }
      })

    })
    
  }
})